I have an action handler for document when its created a media folder is also created in the root of the media section. When document is updated if the media folder does not exist then it also creates the media folder in the root. My code to test if media folder already exists looks like this:
[code]
private int createMediaFolder(Document sender)
{
int id = 0;
try
{
Media root = new Media(Config.Settings.MediaFolderRootID);
id = getExistingFolderID(root, sender.Text);
//media folder does not already exist
if (id == 0)
{
Media m = Media.MakeNew(sender.Text, new MediaType(1031), UmbracoEnsuredPage.CurrentUser, root.Id);
id = m.Id;
}
private int getExistingFolderID(Media root, string folderName)
{
int folderID=0;
foreach (Media m in root.Children) {
if (m.Text == folderName) {
folderID = m.Id;
return folderID;
}
}
return folderID;
}
[/code]
Config.Settings.MediaFolderRootID that is value in config file and is set to -1 which corresponds to Media root folder. The issue is that when i do root.Children it comes back with "SYSTEM DATA: umbraco master root" ie the Media folder and not the children which exist in the root.
root media folder issue [SOLVED]
Guys,
I have an action handler for document when its created a media folder is also created in the root of the media section. When document is updated if the media folder does not exist then it also creates the media folder in the root. My code to test if media folder already exists looks like this:
[code]
private int createMediaFolder(Document sender)
{
int id = 0;
try
{
Media root = new Media(Config.Settings.MediaFolderRootID);
id = getExistingFolderID(root, sender.Text);
//media folder does not already exist
if (id == 0)
{
Media m = Media.MakeNew(sender.Text, new MediaType(1031), UmbracoEnsuredPage.CurrentUser, root.Id);
id = m.Id;
}
}
catch (Exception ex)
{
//log
errors = true;
errorMessage += "\n\n" + ex.Message.ToString();
Log.Add(LogTypes.Error, sender.Id, ex.Message.ToString());
}
return id;
}
private int getExistingFolderID(Media root, string folderName)
{
int folderID=0;
foreach (Media m in root.Children) {
if (m.Text == folderName) {
folderID = m.Id;
return folderID;
}
}
return folderID;
}
[/code]
Config.Settings.MediaFolderRootID that is value in config file and is set to -1 which corresponds to Media root folder. The issue is that when i do root.Children it comes back with "SYSTEM DATA: umbraco master root" ie the Media folder and not the children which exist in the root.
Anyone else had this issue?
Regards
Ismail
Try,
[code]
private int getExistingFolderID(Media root, string folderName)
{
Media[] roots = Media.GetRootMedias();
foreach (Media root in roots)
{
if(root.Text.Equals(MediaRootName))
{
return root.Id
}
}
return 0;
}
[/code]
Adz,
Nice one mate works a treat, as you said in your tweet
[quote]
each root folder is its own tree, and i dont think they share a common parent
[/quote]
Regards
Ismail
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.